package util

import (
	logger "gitee.com/zaper/private-logger"
	"os"
	"os/signal"
	"sync"
	"syscall"
)

var (
	SystemSignalMonitorOnce   sync.Once
	SystemSignalMonitorSingle *SystemSignalMonitor
)

type SystemSignalMonitor struct {
	shutdown chan os.Signal
}

func GetSystemSignalMonitor() *SystemSignalMonitor {
	SystemSignalMonitorOnce.Do(func() {
		SystemSignalMonitorSingle = &SystemSignalMonitor{
			shutdown: make(chan os.Signal, 1),
		}
	})
	return SystemSignalMonitorSingle
}

func (sys SystemSignalMonitor) Monitor() {
	signal.Notify(sys.shutdown, syscall.SIGINT, syscall.SIGTERM)
	sign := <-sys.shutdown
	logger.Printf("Im shutting down, %s", sign)
}
